<?php
/**
 * 认证模块
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\UserService;

use Illuminate\Http\Request;
use DB;
use App\Services\ImgServices\ImgTool;
use App\Services\UserService\UserTool;
use App\Services\ProductsService\ProductTool;
use App\Services\Tool\Jpush\JpushTool;
use Log;

class authenticationService
{
    protected $UserTool;
    protected $Product;

    public function __construct(UserTool $UserTool, ProductTool $Product)
    {
        $this->UserTool = $UserTool;
        $this->Product = $Product;
    }

    /**
     * 整合(暂)
     */
    public function DesignerAuthenticationInfo(Request $request)
    {
        $check = DB::table('designer_authentications')->where('user_id')->first();
        if ($check == null) {
            $result = $this->DesignerAuthenticationRecord($request);
            return $result;
        } else {
            $result = $this->DesignerAuthenticationRecord($request);
            return $result;
        }
    }

    /**
     * 设计师认证信息记录
     */
    public function DesignerAuthenticationRecord(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object)array(), 'state' => 'M09000');
            return $result;
        }
        $user_type = DB::table('users')->where('user_id', $user_id)->value('user_type');
        if ($user_type == 2) {
            $result = array('message' => '认证品牌商后不能再认证设计师哦~', 'result' => (Object)array(), 'state' => 'M09012');
            return $result;
        }
        $state_record_brand_authentication = DB::table('brand_authentications')->where('user_id', $user_id)->value('state');
        if ($state_record_brand_authentication === 0) {
            $result = array('message' => '您的品牌认证正在审核中', 'result' => (Object)array(), 'state' => 'M09012');
            return $result;
        }
        $real_name = $request->get('real_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $real_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($real_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '真实姓名中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $check = $this->checkCompanyName($real_name);

        $gender = $request->get('gender');
        $company = $request->get('company', '');
        if ($company != '') {
            if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company) ||
                preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
                $arr = array('message' => '公司名称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
                return $arr;
            }
        }
        $job = $request->get('job');
        $card_id = $request->get('card_id');
        $phone = $request->get('phone');
        $email = $request->get('email');
        $front_card_url = $request->get('front_card_url');//身份证正面
        $back_card_url = $request->get('back_card_url');//身份证反面
        $create_img_url_1 = $request->get('create_img_url_1');//作品1
        $create_img_url_2 = $request->get('create_img_url_2');//作品2
        $create_img_url_3 = $request->get('create_img_url_3');//作品3

        if (strlen($real_name) == 0 || strlen($gender) == 0 || strlen($job) == 0 ||
            strlen($card_id) == 0 || strlen($phone) == 0 || strlen($email) == 0 ||
            strlen($front_card_url) == 0 || strlen($back_card_url) == 0 ||
            strlen($create_img_url_1) == 0 || strlen($create_img_url_2) == 0 || strlen($create_img_url_3) == 0
        ) {
            $result = array('message' => '请检查信息完整', 'result' => (Object)array(), 'state' => 'M00002');
            return $result;
        }
        $VerifiedCard = $this->UserTool->VerifiedCard($real_name, $card_id);
        if ($VerifiedCard['state'] != 'M00000') {
            if ($VerifiedCard['state'] == 'M00003') {
                $bool = $this->recordCardError($user_id);
                if ($bool == false) {
                    $result = array('message' => '当前验证错误次数已达上限(3次)', 'result' => (Object)array(), 'state' => 'M09022');
                } else {
                    $result = array('message' => $VerifiedCard['message'], 'result' => (Object)array(), 'state' => 'M09024');
                }
                return $result;
            } else {
                $id_card_state = 0;
            }
        } else {
            $id_card_state = 1;
        }

        $check1 = $this->UserTool->checkPhone($phone);
        $check2 = $this->UserTool->checkEmail($email);

        if ($check1 == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }
        if ($check2 == false) {
            $result = array('message' => '电子邮箱格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }

        $fans_num = DB::table('follows')->where('follow_user_id', $user_id)->where('state', 1)->count();
        $product_num = DB::table('products')->where([
            'user_id' => $user_id,
            'is_shelves' => 1,
            'state' => 1
        ])->count();
        //检查是否存在认证信息
        $hasAuthentication = DB::table('designer_authentications')->where('user_id', $user_id)->first();
        if ($hasAuthentication != null) {
            $state = $hasAuthentication->state;
            if ($state == 0) {
                $result = array('message' => '您的认证信息审核中,请勿重复提交', 'result' => (Object)array(), 'state' => 'M09013');
                return $result;
            } else if ($state == 2) {
                $result = array('message' => '您的认证已通过,请勿重复提交', 'result' => (Object)array(), 'state' => 'M09014');
                return $result;
            }
        }

        //绝对路径转化相对路径
        $front_card_path = ImgTool::getImgPathByImgUrl($front_card_url);
        $back_card_path = ImgTool::getImgPathByImgUrl($back_card_url);
        $back_create_1_path = ImgTool::getImgPathByImgUrl($create_img_url_1);
        $back_create_2_path = ImgTool::getImgPathByImgUrl($create_img_url_2);
        $back_create_3_path = ImgTool::getImgPathByImgUrl($create_img_url_3);

        DB::table('designer_authentications')->insert(
            [
                'user_id' => $user_id,
                'real_name' => $real_name,
                'gender' => $gender,
                'company' => $company,
                'job' => $job,
                'card_id' => $card_id,
                'phone' => $phone,
                'email' => $email,
                'front_card_url' => $front_card_path,
                'back_card_url' => $back_card_path,
                'create_img_url_1' => $back_create_1_path,
                'create_img_url_2' => $back_create_2_path,
                'create_img_url_3' => $back_create_3_path,
                'fans_num' => $fans_num,
                'product_num' => $product_num,
                'message' => '',
                'id_card_state' => $id_card_state,
                'state' => 0,
                'created_at' => time(),
                'updated_at' => time(),
            ]
        );
        $message = '认证审核中';
        $result = JpushTool::push($message, 0, $user_id,
            [
                'title' => '认证审核中',
                'user_id' => $user_id,
                'class' => 'system_notification',
                'type' => 1,
                'jump_value' => '',
                'template_no' => 2,
                'message' => '身份认证申请已提交成功，预计三个工作日内完成审核，请耐心等待～',
                'images' => ['original_image' => '',
                    'thumbnail' => '']
            ]);
        $data = array('authentication_state' => 0);
        $result = array('message' => '记录成功,请等待审核', 'result' => $data, 'state' => 'M00000');
        return $result;
    }


    /**
     * 品牌认证信息记录
     */
    public function BrandAuthenticationRecord(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object)array(), 'state' => 'M09000');
            return $result;
        }
        $user_type = DB::table('users')->where('user_id', $user_id)->value('user_type');
        if ($user_type == 1) {
            $result = array('message' => '认证设计师后不能再认证品牌商哦~', 'result' => (Object)array(), 'state' => 'M09015');
            return $result;
        }
        $state_record_designer_authentication = DB::table('designer_authentications')->where('user_id', $user_id)->value('state');
        if ($state_record_designer_authentication === 0) {
            $result = array('message' => '您的设计师认证正在审核中', 'result' => (Object)array(), 'state' => 'M09012');
            return $result;
        }
        $company_name = $request->get('company_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '公司全称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $company_abbreviation = $request->get('company_abbreviation');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company_abbreviation) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company_abbreviation)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '公司简称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $checkCompany1 = $this->checkCompanyName($company_name);
        $checkCompany2 = $this->checkCompanyName($company_abbreviation);
        if ($checkCompany1 == true) {
            $result = array('message' => '该公司全称为系统保留关键词', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        if ($checkCompany2 == true) {
            $result = array('message' => '该公司简称为系统保留关键词', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        $checkBrand_company_name = DB::table('brand_authentications')->where(['company_name' => $company_name])->first();
        $checkBrand_company_abbreviation = DB::table('brand_authentications')->where(['company_abbreviation' => $company_name])->first();
        if ($checkBrand_company_name != null) {
            $result = array('message' => '该品牌名已被申请审核或已存在', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        if ($checkBrand_company_abbreviation != null) {
            $result = array('message' => '该品牌简称已被申请审核或已存在', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        $manager_name = $request->get('manager_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $manager_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($manager_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '管理员姓名中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $job = $request->get('job');
        $card_id = $request->get('card_id');
        $phone = $request->get('phone');

        $front_card_url = $request->get('front_card_url');//身份证正面
        $back_card_url = $request->get('back_card_url');//身份证反面
        $business_license_url = $request->get('business_license_url');//营业执照
        $business_letters_url = $request->get('business_letters_url');//企业公函

        if (strlen($company_name) == 0 || strlen($company_abbreviation) == 0 || strlen($manager_name) == 0 ||
            strlen($card_id) == 0 || strlen($phone) == 0 || strlen($job) == 0 ||
            strlen($front_card_url) == 0 || strlen($back_card_url) == 0 ||
            strlen($business_license_url) == 0 || strlen($business_letters_url) == 0
        ) {
            $result = array('message' => '请检查信息完整', 'result' => (Object)array(), 'state' => 'M00002');
            return $result;
        }

        $VerifiedCard = $this->UserTool->VerifiedCard($manager_name, $card_id);
        if ($VerifiedCard['state'] != 'M00000') {
            if ($VerifiedCard['state'] == 'M00003') {
                $bool = $this->recordCardError($user_id);
                if ($bool == false) {
                    $result = array('message' => '当前验证错误次数已达上限(3次)', 'result' => (Object)array(), 'state' => 'M09022');
                } else {
                    $result = array('message' => $VerifiedCard['message'], 'result' => (Object)array(), 'state' => 'M09024');
                }
                return $result;
            } else {
                $id_card_state = 0;
            }
        } else {
            $id_card_state = 1;
        }

        $check1 = $this->UserTool->checkPhone($phone);
        if ($check1 == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }

        $product_num = DB::table('products')->where([
            'user_id' => $user_id,
            'is_shelves' => 1,
            'state' => 1
        ])->count();
        $fans_num = DB::table('follows')->where('follow_user_id', $user_id)->where('state', 1)->count();

        //检查是否存在认证信息
        $hasAuthentication = DB::table('brand_authentications')->where('user_id', $user_id)->first();
        if ($hasAuthentication != null) {
            $state = $hasAuthentication->state;
            if ($state == 0) {
                $result = array('message' => '您的认证信息审核中,请勿重复提交', 'result' => (Object)array(), 'state' => 'M09013');
                return $result;
            } else if ($state == 2) {
                $result = array('message' => '您的认证已通过,请勿重复提交', 'result' => (Object)array(), 'state' => 'M09014');
                return $result;
            }
        }

        //绝对路径转化相对路径
        $front_card_path = ImgTool::getImgPathByImgUrl($front_card_url);
        $back_card_path = ImgTool::getImgPathByImgUrl($back_card_url);
        $Valid_business_license_path = ImgTool::getImgPathByImgUrl($business_license_url);
        $Valid_business_letters_path = ImgTool::getImgPathByImgUrl($business_letters_url);

        DB::table('brand_authentications')->insert(
            [
                'user_id' => $user_id,
                'company_name' => $company_name,
                'company_abbreviation' => $company_abbreviation,
                'manager_name' => $manager_name,
                'job' => $job,
                'card_id' => $card_id,
                'phone' => $phone,
                'front_card_url' => $front_card_path,
                'back_card_url' => $back_card_path,
                'business_license_url' => $Valid_business_license_path,
                'business_letters_url' => $Valid_business_letters_path,
                'fans_num' => $fans_num,
                'product_num' => $product_num,
                'id_card_state' => $id_card_state,
                'message' => '',
                'state' => 0,
                'created_at' => time(),
                'updated_at' => time(),
            ]
        );
        $message = '认证审核中';
        $result = JpushTool::push($message, 0, $user_id,
            [
                'title' => '认证审核中',
                'user_id' => $user_id,
                'class' => 'system_notification',
                'type' => 1,
                'jump_value' => '',
                'template_no' => 2,
                'message' => '身份认证申请已提交成功，预计三个工作日内完成审核，请耐心等待～',
                'images' => ['original_image' => '',
                    'thumbnail' => '']
            ]);
        $data = array('authentication_state' => 0);
        $result = array('message' => '记录成功,请等待审核', 'result' => $data, 'state' => 'M00000');
        return $result;
    }

    /**
     * 设计师认证信息修改
     */
    public function DesignerAuthenticationEdit(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object)array(), 'state' => 'M09000');
            return $result;
        }
        $real_name = $request->get('real_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $real_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($real_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '真实姓名中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $gender = $request->get('gender');
        $company = $request->get('company', '');
        if($company != ''){
            if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company) ||
                preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
                $arr = array('message' => '公司名称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
                return $arr;
            }
        }
        $job = $request->get('job');
        $card_id = $request->get('card_id');
        $phone = $request->get('phone');
        $email = $request->get('email');
        $front_card_url = $request->get('front_card_url');
        $back_card_url = $request->get('back_card_url');
        $create_img_url_1 = $request->get('create_img_url_1');
        $create_img_url_2 = $request->get('create_img_url_2');
        $create_img_url_3 = $request->get('create_img_url_3');
        if (strlen($real_name) == 0 || strlen($gender) == 0 || strlen($job) == 0 ||
            strlen($card_id) == 0 || strlen($phone) == 0 || strlen($email) == 0 ||
            strlen($front_card_url) == 0 || strlen($back_card_url) == 0 ||
            strlen($create_img_url_1) == 0 || strlen($create_img_url_2) == 0 || strlen($create_img_url_3) == 0
        ) {
            $result = array('message' => '请检查信息完整', 'result' => (Object)array(), 'state' => 'M00002');
            return $result;
        }

        $VerifiedCard = $this->UserTool->VerifiedCard($real_name, $card_id);
        if ($VerifiedCard['state'] != 'M00000') {
            if ($VerifiedCard['state'] == 'M00003') {
                $bool = $this->recordCardError($user_id);
                if ($bool == false) {
                    $result = array('message' => '当前验证错误次数已达上限(3次)', 'result' => (Object)array(), 'state' => 'M09022');
                } else {
                    $result = array('message' => $VerifiedCard['message'], 'result' => (Object)array(), 'state' => 'M09024');
                }
                return $result;
            } else {
                $id_card_state = 0;
            }
        } else {
            $id_card_state = 1;
        }

        $Valid_front_card_url = $this->UserTool->ValidImage($front_card_url);
        $Valid_back_card_url = $this->UserTool->ValidImage($back_card_url);
        $Valid_create_img_url_1 = $this->UserTool->ValidImage($create_img_url_1);
        $Valid_create_img_url_2 = $this->UserTool->ValidImage($create_img_url_2);
        $Valid_create_img_url_3 = $this->UserTool->ValidImage($create_img_url_3);
        if ($Valid_front_card_url == false) {
            $result = array('message' => '身份证正面照请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_back_card_url == false) {
            $result = array('message' => '身份证反面照请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_create_img_url_1 == false) {
            $result = array('message' => '作品1图片请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_create_img_url_2 == false) {
            $result = array('message' => '作品2图片请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_create_img_url_3 == false) {
            $result = array('message' => '作品3图片请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }

        $check1 = $this->UserTool->checkPhone($phone);
        $check2 = $this->UserTool->checkEmail($email);

        if ($check1 == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }
        if ($check2 == false) {
            $result = array('message' => '电子邮箱格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }

        //绝对路径转化相对路径
        $front_card_path = ImgTool::getImgPathByImgUrl($front_card_url);
        $back_card_path = ImgTool::getImgPathByImgUrl($back_card_url);
        $back_create_1_path = ImgTool::getImgPathByImgUrl($create_img_url_1);
        $back_create_2_path = ImgTool::getImgPathByImgUrl($create_img_url_2);
        $back_create_3_path = ImgTool::getImgPathByImgUrl($create_img_url_3);

        DB::table('designer_authentications')->where('user_id', $user_id)->update(
            [
                'user_id' => $user_id,
                'real_name' => $real_name,
                'gender' => $gender,
                'company' => $company,
                'job' => $job,
                'card_id' => $card_id,
                'phone' => $phone,
                'email' => $email,
                'front_card_url' => $front_card_path,
                'back_card_url' => $back_card_path,
                'create_img_url_1' => $back_create_1_path,
                'create_img_url_2' => $back_create_2_path,
                'create_img_url_3' => $back_create_3_path,
                'message' => '',
                'id_card_state' => $id_card_state,
                'state' => 0,
                'created_at' => time(),
                'updated_at' => time(),
            ]
        );
        $message = '认证审核中（身份认证申请已提交成功，预计三个工作日内完成审核，请耐心等待～）';
        $result = JpushTool::push($message, 0, $user_id,
            [
                'title' => '认证消息',
                'user_id' => $user_id,
                'class' => 'system_notification',
                'type' => 1,
                'jump_value' => '',
                'template_no' => 2,
                'message' => $message,
                'images' => ['original_image' => '',
                    'thumbnail' => '']
            ]);
        $data = array('authentication_state' => 0);
        $result = array('message' => '修改成功,请等待审核', 'result' => $data, 'state' => 'M00000');
        return $result;
    }


    /**
     * 品牌认证信息修改
     */
    public function BrandAuthenticationEdit(Request $request)
    {
        $user_id = $request->get('user_id');
        if (strlen($user_id) == 0) {
            $result = array('message' => '请先登录', 'result' => (Object)array(), 'state' => 'M09000');
            return $result;
        }
        $company_name = $request->get('company_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '公司名称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $company_abbreviation = $request->get('company_abbreviation');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $company_abbreviation) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($company_abbreviation)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '公司简称中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $checkBrand_company_name = DB::table('brand_authentications')->whereNotIn('user_id', [$user_id])->where(['company_name' => $company_name])->first();
        $checkBrand_company_abbreviation = DB::table('brand_authentications')->whereNotIn('user_id', [$user_id])->where('company_abbreviation', $company_name)->first();
        if ($checkBrand_company_name != null) {
            $result = array('message' => '该品牌名已被申请审核或已存在', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        if ($checkBrand_company_abbreviation != null) {
            $result = array('message' => '该品牌简称已被申请审核或已存在', 'result' => (Object)array(), 'state' => 'M09029');
            return $result;
        }
        $manager_name = $request->get('manager_name');
        if (!preg_match("/[\x{4e00}-\x{9fa5}\w]+$/u", $manager_name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i",json_encode($manager_name)) != 0) { //不允许特殊字符  后一个正则为判断emoji表情
            $arr = array('message' => '管理员姓名中不能含有特殊字符', 'result' => (object)array(), 'state' => 'M09009');
            return $arr;
        }
        $job = $request->get('job');
        $card_id = $request->get('card_id');
        $phone = $request->get('phone');

        $front_card_url = $request->get('front_card_url');//身份证正面
        $back_card_url = $request->get('back_card_url');//身份证反面
        $business_license_url = $request->get('business_license_url');//营业执照
        $business_letters_url = $request->get('business_letters_url');//企业公函
        if (strlen($company_name) == 0 || strlen($company_abbreviation) == 0 || strlen($manager_name) == 0 ||
            strlen($card_id) == 0 || strlen($phone) == 0 || strlen($job) == 0 ||
            strlen($front_card_url) == 0 || strlen($back_card_url) == 0 ||
            strlen($business_license_url) == 0 || strlen($business_letters_url) == 0
        ) {
            $result = array('message' => '请检查信息完整', 'result' => (Object)array(), 'state' => 'M00002');
            return $result;
        }
        $VerifiedCard = $this->UserTool->VerifiedCard($manager_name, $card_id);
        if ($VerifiedCard['state'] != 'M00000') {
            if ($VerifiedCard['state'] == 'M00003') {
                $bool = $this->recordCardError($user_id);
                if ($bool == false) {
                    $result = array('message' => '当前验证错误次数已达上限(3次)', 'result' => (Object)array(), 'state' => 'M09022');
                } else {
                    $result = array('message' => $VerifiedCard['message'], 'result' => (Object)array(), 'state' => 'M09024');
                }
                return $result;
            } else {
                $id_card_state = 0;
            }
        } else {
            $id_card_state = 1;
        }


        $Valid_front_card_url = $this->UserTool->ValidImage($front_card_url);
        $Valid_back_card_url = $this->UserTool->ValidImage($back_card_url);
        $Valid_business_license_url = $this->UserTool->ValidImage($business_license_url);
        $Valid_business_letters_url = $this->UserTool->ValidImage($business_letters_url);
        if ($Valid_front_card_url == false) {
            $result = array('message' => '身份证正面照请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_back_card_url == false) {
            $result = array('message' => '身份证反面照请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_business_license_url == false) {
            $result = array('message' => '营业执照照片请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }
        if ($Valid_business_letters_url == false) {
            $result = array('message' => '企业公函照片请重新上传', 'result' => (Object)array(), 'state' => 'M09021');
            return $result;
        }

        $check1 = $this->UserTool->checkPhone($phone);
        if ($check1 == false) {
            $result = array('message' => '手机号码格式不正确', 'result' => (Object)array(), 'state' => 'M09020');
            return $result;
        }

        //绝对路径转化相对路径
        $front_card_path = ImgTool::getImgPathByImgUrl($front_card_url);
        $back_card_path = ImgTool::getImgPathByImgUrl($back_card_url);
        $Valid_business_license_path = ImgTool::getImgPathByImgUrl($business_license_url);
        $Valid_business_letters_path = ImgTool::getImgPathByImgUrl($business_letters_url);

        DB::table('brand_authentications')->where('user_id', $user_id)->update(
            [
                'user_id' => $user_id,
                'company_name' => $company_name,
                'company_abbreviation' => $company_abbreviation,
                'manager_name' => $manager_name,
                'job' => $job,
                'card_id' => $card_id,
                'phone' => $phone,
                'front_card_url' => $front_card_path,
                'back_card_url' => $back_card_path,
                'business_license_url' => $Valid_business_license_path,
                'business_letters_url' => $Valid_business_letters_path,
                'id_card_state' => $id_card_state,
                'message' => '',
                'state' => 0,
                'created_at' => time(),
                'updated_at' => time(),
            ]
        );
        $message = '认证审核中（身份认证申请已提交成功，预计三个工作日内完成审核，请耐心等待～）';
        $result = JpushTool::push($message, 0, $user_id,
            [
                'title' => '认证消息',
                'user_id' => $user_id,
                'class' => 'system_notification',
                'type' => 1,
                'jump_value' => '',
                'template_no' => 2,
                'message' => $message,
                'images' => ['original_image' => '',
                    'thumbnail' => '']
            ]);
        $data = array('authentication_state' => 0);
        $result = array('message' => '修改成功,请等待审核', 'result' => $data, 'state' => 'M00000');
        return $result;
    }


    /**
     * 身份证验证错误 记一次错误  一天只允许三次
     */
    public function recordCardError($user_id)
    {
        $hasSava = DB::table('card_error_nums')->where('user_id', $user_id)->first();
        if ($hasSava != null) {
            $num = $hasSava->card_id_error_num;
            if ($num == 3) {
                return false;
            }
            DB::table('card_error_nums')->where('user_id', $user_id)->update(['card_id_error_num' => $num + 1]);
            return true;
        } else {
            DB::table('card_error_nums')->insert(
                [
                    'user_id' => $user_id,
                    'card_id_error_num' => 1,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]
            );
            return true;
        }
    }

    public function checkCompanyName($name)
    {
        $name = strtolower($name);
        if ($name != '') {
            $lists = DB::table('company_name_limits')
                ->where('keyword', $name)
                ->select('keyword')
                ->get();
            foreach ($lists as $item){
                $keyword = strtolower($item->keyword);
                if($name == $keyword){
                    return true;
                }
            }
        }
    }

}